home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.07 Jul 87 / bit map source / firstdrag.p < prev    next >
Encoding:
Text File  |  1987-03-22  |  2.9 KB  |  120 lines  |  [TEXT/MPS ]

  1. program drag;
  2.  
  3. USES {$LOAD pinterfaces.dump}        
  4.         MemTypes,QuickDraw,OsIntf,PasLibIntf,ToolIntf,
  5.         PackIntf,IntEnv,CursorCtl;
  6.  
  7. var myPic : PicHandle;
  8.     myRect : Rect;
  9.     oldPort: GrafPtr;
  10.  
  11. procedure DragIt( thePicture : PicHandle );
  12. var
  13.     underBits, pictureBits : BitMap;
  14.     where : Point;
  15.     thePictureRgn : RgnHandle;
  16.     wMgrPort,
  17.     oldPort,
  18.     workPort : GrafPtr;
  19.     dragRect,
  20.     tempBounds : Rect;
  21.     mousePt, testPt : Point;
  22.     synchCount : longint;
  23. begin
  24.     {remember where we are}
  25.     GetPort( oldPort );
  26.     GetWMgrPort( wMgrPort );
  27.     
  28.     {try to allocate and erase the bitmaps}
  29.     tempBounds := thePicture^^.picFrame;
  30.     with underBits, tempBounds do
  31.     begin
  32.         rowBytes := ((right - left + 15) DIV 16) * 2;
  33.         baseAddr := NewPtr(rowBytes * (bottom - top));
  34.         bounds := tempBounds;
  35.         if MemError <> noErr then
  36.             baseAddr := nil;
  37.         if baseAddr = nil then IEExit(1);
  38.     end;
  39.     with pictureBits, tempBounds do
  40.     begin
  41.         rowBytes := ((right - left + 15) DIV 16) * 2;
  42.         baseAddr := NewPtr(rowBytes * (bottom - top));
  43.         bounds := tempBounds;
  44.         if MemError <> noErr then
  45.             baseAddr := nil;
  46.         if baseAddr = nil then IEExit(1);
  47.     end;
  48.  
  49.     workPort := GrafPtr( NewPtr( sizeof(GrafPort) ) );
  50.     if workPort = nil then IEExit(1);
  51.     
  52.     OpenPort( workPort );
  53.     SetPortBits( underBits );
  54.     EraseRect( underBits.bounds );
  55.     SetPortBits( pictureBits );
  56.     EraseRect( pictureBits.bounds );
  57.     
  58.     {draw the picture into pictureBits & create thePictureRgn}
  59.     thePictureRgn := NewRgn;
  60.     OpenRgn;
  61.         DrawPicture( thePicture, thePicture^^.picFrame );
  62.     CloseRgn( thePictureRgn );
  63.     DrawPicture( thePicture, thePicture^^.picFrame );
  64.     
  65.     {these are the rectangles for use in CopyBits}
  66.     dragRect := pictureBits.bounds;
  67.     
  68.     SetPort( wMgrPort );
  69.     while button do
  70.     begin
  71.         GetMouse( mousePt );
  72.         OffSetRgn( thePictureRgn, -dragRect.left, -dragRect.top );
  73.         OffSetRgn( thePictureRgn, mousePt.h, mousePt.v );
  74.         OffSetRect( dragRect, -dragRect.left, -dragRect.top );
  75.         OffSetRect( dragRect, mousePt.h, mousePt.v );
  76.  
  77.     {save bits underneath}
  78.         CopyBits( screenBits, underBits, dragRect, underBits.bounds, 
  79.                 srcCopy,nil);
  80.     {mask out shape of my object}
  81.         CopyBits( pictureBits, screenBits, pictureBits.bounds, dragRect,
  82.                 notSrcBic, thePictureRgn );
  83.     {draw my object}
  84.         CopyBits( pictureBits, screenBits, pictureBits.bounds, dragRect,
  85.                 srcOr, thePictureRgn );
  86.         repeat 
  87.             GetMouse( testPt );
  88.         until not EqualPt( testPt, mousePt );
  89.         
  90.         synchCount := TickCount;
  91.         repeat until TickCount <> synchCount;
  92.     {restore bits underneath}
  93.         CopyBits( underBits, screenBits, underBits.bounds, dragRect, 
  94.                 srcCopy, nil);
  95.     end; {while button}
  96.  
  97.     {tidy up}
  98.     SetPort( oldPort );
  99.     ClosePort( workPort );
  100.     DisposPtr( Pointer(workPort) );
  101.     DisposPtr( pictureBits.baseAddr );
  102.     DisposPtr( underBits.baseAddr );
  103.     DisposHandle( Handle(thePictureRgn) );
  104.  
  105. end; {DragIt}
  106.  
  107. begin{main}
  108.     InitGraf(@thePort);
  109.     GetWMgrPort( oldPort );
  110.     SetPort( oldPort );
  111.     
  112.     ClipRect( screenBits.bounds );
  113.     myPic := PicHandle(GetResource('PICT', 128));
  114.     
  115.     InitCursor;
  116.     repeat until button;
  117.     DragIt( myPic );
  118.     IEExit(0);
  119. end.
  120.